package com.bussiness;
/*
 * Author: Deepika Gohil
 */

import com.exceptions.*;

import javax.jws.WebService;

import com.beans.Movie;
import com.beans.Person;
import com.dao.moviedao;

@WebService
public class MovieBussiness 
{
	/* 
	 * get list of all the Category IDs.
	 */
	public int[] getAllCatIds() throws ServiceUnavailableException
	{
		int[] catagoryIds = null;
		moviedao moviedaoObject = new moviedao();
		try
		{
			catagoryIds = moviedaoObject.getAllCatIds();
		}
		catch(ServiceUnavailableException suex)
		{
			suex.printStackTrace();
			System.out.println("\n Service Unavailable.");
			throw suex;	
		}
		return catagoryIds;
	}
	
	
	/* 
	 * get the category Names depending on the category ID. 
	 */
	public String getCategoryName(int categoryId) throws ServiceUnavailableException, InvalidCategoryIdException
	{
		String CategoryName="";
		moviedao moviedaoObject = new moviedao();
		try
		{
			CategoryName = moviedaoObject.getCategoryName(categoryId);
		}
		catch(InvalidCategoryIdException invalidid)
		{
			invalidid.printStackTrace();
			System.out.println("\n Invalid Category Id Exception.");
			throw invalidid;		
		}
		catch(ServiceUnavailableException suex)
		{
			suex.printStackTrace();
			System.out.println("\n Service Unavailable.");
			throw suex;	
		}
		return CategoryName;	
	}
	
	/* 
	 * get list of all the movies.
	 */
	public Movie[] getAllMovies() throws ServiceUnavailableException
	{
		Movie[] movieList= null;
		
		moviedao moviedaoObject = new moviedao();
		try
		{
			movieList = moviedaoObject.getAllMovies();
		}
		catch(ServiceUnavailableException suex)
		{
			suex.printStackTrace();
			System.out.println("\n Service Unavailable.");
			throw suex;	
		}
		return movieList;
	}
	
	/* 
	 * get list of all the movies depending on the category. 
	 */
	public Movie[] getMoviesbyCategory(int categoryId) throws ServiceUnavailableException
	{
		Movie[] movieList= null;
		moviedao moviedaoObject = new moviedao();
		try
		{
			movieList = moviedaoObject.getCategoryMovies(categoryId);
		}
		catch(ServiceUnavailableException suex)
		{
			suex.printStackTrace();
			System.out.println("\n Service Unavailable.");
			throw suex;	
		}
		return movieList;
	}
	
	/*
	 * Create a new movie and add it to the database
	 * It is performed by the Administration unit
	 */
	public int createMovie(Movie addMovie) throws ServiceUnavailableException,MovieAlreadyExistsException, InvalidEnteriesException
	{
		int isValid = 0;
		moviedao moviedaoObject = new moviedao();
		try
		{
			isValid = moviedaoObject.createMovie(addMovie);
		}
		catch(ServiceUnavailableException suex)
		{
			suex.printStackTrace();
			System.out.println("\n Service Unavailable.");
			throw suex;	
		}
		catch(MovieAlreadyExistsException ex1)
		{
			ex1.printStackTrace();
			System.out.println("\n Movie Already Exists Exception.");
			throw ex1;	
		}
		catch(InvalidEnteriesException ex2)
		{
			ex2.printStackTrace();
			System.out.println("\n Invalid movie detail entries.");
			throw ex2;	
		}
		return isValid;
	}

	/*
	 * update the movie.
	 * It can be used by the administration to update the type.
	 * Or It can also be used to update the ratings and number of ratings.
	 */
	public int updateMovie(Movie editMovie) throws ServiceUnavailableException, MovieAlreadyExistsException, InvalidEnteriesException
	{
		int isValid = 0;
		moviedao moviedaoObject = new moviedao();
		try
		{
			isValid = moviedaoObject.updateMovie(editMovie);
		}
		catch(ServiceUnavailableException suex)
		{
			suex.printStackTrace();
			System.out.println("\n Service Unavailable.");
			throw suex;	
		}
		catch(MovieAlreadyExistsException maeex)
		{
			maeex.printStackTrace();
			System.out.println("\n Movie details Matched with some movie in the database!");
			throw maeex;	
		}
		catch(InvalidEnteriesException ex2)
		{
			ex2.printStackTrace();
			System.out.println("\n Invalid movie detail entries.");
			throw ex2;	
		}
		return isValid;
	}
	
	/*
	 * Deletes an existing movie from the database!
	 * It is performed by the Administration unit.
	 */
	public int deleteMovie(int movieId) throws ServiceUnavailableException, PendingIssuedMoviesException, MovieNotExistsException
	{
		int isValid = 0;
		moviedao moviedaoObject = new moviedao();
		try
		{
			isValid = moviedaoObject.deleteMovie(movieId);
		}
		catch(ServiceUnavailableException suex)
		{
			suex.printStackTrace();
			System.out.println("\n Service Unavailable.");
			throw suex;	
		}catch(PendingIssuedMoviesException pmex)
		{
		pmex.printStackTrace();
		System.out.println("\n Movie can not be deleted! Pending issued movies!");	
		throw pmex;
		}
		catch(MovieNotExistsException mnotexistex)
		{
		mnotexistex.printStackTrace();
		System.out.println("\n Movie can not be deleted! Pending issued movies!");	
		throw mnotexistex;
		}
		return isValid;
	}
	
	/*
	 * Get the movie from the system and issue it.
	 */
	public int issueMovie(int MovieId,long MembershipId) throws MovieUnavailableException, UserNotEligibleException, ServiceUnavailableException, MovieAlreadyIssuedException, MovieNotExistsException
	{
		int isValid = 0;
		moviedao moviedaoObject = new moviedao();
		try
		{
			isValid = moviedaoObject.issueMovie(MovieId, MembershipId);
		}
		catch(ServiceUnavailableException suex)
		{
			suex.printStackTrace();
			System.out.println("\n Service Unavailable.");
			throw suex;	
		}catch(MovieUnavailableException munEx)
		{
			munEx.printStackTrace();
			System.out.println("\n Service Unavailable.");
			throw munEx;	
		}catch(UserNotEligibleException uneEx)
		{
			uneEx.printStackTrace();
			System.out.println("\n Service Unavailable.");
			throw uneEx;		
		}catch(MovieAlreadyIssuedException madEx)
		{
			madEx.printStackTrace();
			System.out.println("\n Service Unavailable.");
			throw madEx;	
		}
		
		return isValid;
	}
	
	/*
	 * Customer returns the movie to the system. 
	 */
	public int submitMovie(int movieId,long membershipId) throws ServiceUnavailableException, MovieNotIssuedtoUserException
	{
		int isValid = 0;
		moviedao moviedaoObject = new moviedao();
		try
		{
			isValid = moviedaoObject.submitMovie(movieId, membershipId);
		}
		catch(ServiceUnavailableException suex)
		{
			suex.printStackTrace();
			System.out.println("\n Service Unavailable.");
			throw suex;	
		}catch(MovieNotIssuedtoUserException munEx)
		{
			munEx.printStackTrace();
			System.out.println("\n Service Unavailable.");
			throw munEx;	
		}
		return isValid;
	}
	
	/*
	 * Generates the bill amount for the Simple customer
	 */
	public Double generateBillSimple(long membershipId, Movie[] movieList) throws ServiceUnavailableException
	{
		Double Balance = 0.0;
		moviedao moviedaoObject = new moviedao();
		try
		{
			Balance = moviedaoObject.generateBillSimple(membershipId,movieList);
		}
		catch(ServiceUnavailableException suex)
		{
			suex.printStackTrace();
			System.out.println("\n Service Unavailable.");
			throw suex;	
		}
		return Balance;
	}
	
	public Movie[] generateBillPremium(long membershipId) throws ServiceUnavailableException
	{
		Movie[] movieList= null;
		
		moviedao moviedaoObject = new moviedao();
		try
		{
			movieList = moviedaoObject.generateBillPremium(membershipId);
			System.out.println("In generateBillPremium :"+movieList.length);
		}
		catch(ServiceUnavailableException suex)
		{
			suex.printStackTrace();
			System.out.println("\n Service Unavailable.");
			throw suex;	
		}
		return movieList;
	}
	
	public Movie getMovieById(int MovieId) throws ServiceUnavailableException, MovieNotExistsException
	{
		Movie m = null;
		moviedao moviedaoObject = new moviedao();
		try
		{
		m = moviedaoObject.getMovieById(MovieId);
		}
		catch(ServiceUnavailableException suex)
		{
			suex.printStackTrace();
			System.out.println("\n Service Unavailable.");
			throw suex;	
		}
		catch(MovieNotExistsException mnotex)
		{
			mnotex.printStackTrace();
			System.out.println("\n Movie with the specific movie id doesn't exists in the database!");
			throw mnotex;	
		}
		return m;
	}
	
	/*
	 * Get the user has already issued the movie or not!!!
	 */
	public int movieAlreadyIssued(int movieId, long membershipId) throws ServiceUnavailableException
	{
		int result =0;
		moviedao moviedaoObject = new moviedao();
		try
		{
		result = moviedaoObject.movieAlreadyIssued(movieId,membershipId);
		}
		catch(ServiceUnavailableException suex)
		{
			suex.printStackTrace();
			System.out.println("\n Service Unavailable.");
			throw suex;	
		}
		return result;
	}
	
	public int isMovieAvailable(int MovieId) throws MovieUnavailableException,ServiceUnavailableException
	{
		int isAvailable = 0;
		moviedao moviedaoObject = new moviedao();
		try
		{
		isAvailable = moviedaoObject.isMovieAvailable(MovieId);
		}
		catch(ServiceUnavailableException suex)
		{
			suex.printStackTrace();
			System.out.println("\n Service Unavailable.");
			throw suex;	
		}
		return isAvailable;
	}
	
	public int isUserEligible(long membershipId, int num_movies) throws UserNotEligibleException,ServiceUnavailableException	
	{
	int isEligible = 0;
	moviedao moviedaoObject = new moviedao();
	try
	{
		isEligible = moviedaoObject.isUserEligible(membershipId, num_movies);
	}catch(ServiceUnavailableException suex)
	{
		suex.printStackTrace();
		System.out.println("\n Service Unavailable.");
		throw suex;	
	}
	return isEligible;
	}
	
	public Movie[] getUserIssuedMovies(long membershipId) throws ServiceUnavailableException, UserHasNotIssuedAnyMovieException
	{
		Movie[] movieList= null;
		
		moviedao moviedaoObject = new moviedao();
		try
		{
			movieList = moviedaoObject.getUserIssuedMovies(membershipId);
		}
		catch(ServiceUnavailableException suex)
		{
			suex.printStackTrace();
			System.out.println("\n Service Unavailable.");
			throw suex;	
		}
		catch(UserHasNotIssuedAnyMovieException uhnimex)
		{
			uhnimex.printStackTrace();
			System.out.println("\n Service Unavailable.");
			throw uhnimex;	
		}
		return movieList;	
	}
}
